// 封装一个get请求的方法
function Ajax(data) {
    let { url, type, params } = data;
    return new Promise(function (resolve, reject) {
        var XHR = XMLHttpRequest ? new XMLHttpRequest() : new window.ActiveXObject('Microsoft.XMLHTTP');

        XHR.onreadystatechange = function () {
            //readyState属性表示请求/响应过程的当前活动阶段。
            if (XHR.readyState == 4) {
                if ((XHR.status >= 200 && XHR.status < 300) || XHR.status == 304) {
                    try {
                        //获取数据
                        var response = JSON.parse(XHR.responseText);
                        resolve(response);
                    } catch (e) {
                        reject(e);
                    }
                } else {
                    reject(new Error("Request was unsuccessful: " + XHR.statusText));
                }
            }
        }

        let dataStr;
        if (typeof params == 'object' && params !== null) {
            let separator = '';
            dataStr = '';
            for (let key in params) {
                if (params.hasOwnProperty(key)) {
                    dataStr += `${separator}${key}=${params[key]}`;
                    separator = '&';
                }
            }
        } else {
            dataStr = '';
        }

        if (type == 'post') {
            XHR.open('post', url);
            XHR.setReuquestHeader('ContentType', 'application/x-wwww-form-urlencoded');
            XHR.send(dataStr);
        } else if (type == 'get') {
            if (dataStr !== '') {
                XHR.open('GET', url + '?' + dataStr, true);
            } else {
                XHR.open('GET', url, true);
            }

            XHR.send(null);
        }
    })
}

// 封装一个日期格式化的请求处理工具函数

function changeTime(time){
    var commonTime = "";
    if(time){
        var unixTimestamp = new Date(time*1) ;
        commonTime = unixTimestamp.toLocaleString();
    }
     return commonTime;
}